package co.yixiang.modules.shop.rest;

import co.yixiang.aop.log.Log;
import co.yixiang.modules.shop.domain.YxProductBase;
import co.yixiang.modules.shop.service.YxProductBaseService;
import co.yixiang.modules.shop.service.dto.YxProductBaseQueryCriteria;
import co.yixiang.modules.shop.utils.IOUtil;
import co.yixiang.utils.FileUtil;
import co.yixiang.utils.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;

/**
 * @author frq
 * @date 2020-06-09
 */
@Slf4j
@RestController
@RequestMapping("/api/yxProductBase")
@Api(value = "外部商品基本数据配置", description = "外部商品基本数据配置")
public class YxProductBaseController {

    @Autowired
    private YxProductBaseService yxProductBaseService;

    @Log("外部商品基本数据配置列表")
    @GetMapping
    @ApiOperation(value = "外部商品基本数据配置列表", notes = "外部商品基本数据配置列表")
    public ResponseEntity getProductBase(YxProductBaseQueryCriteria criteria, Pageable pageable) {
        return new ResponseEntity(yxProductBaseService.findAll(criteria, pageable), HttpStatus.OK);
    }

    @Log("导入文件")
    @PostMapping
    @ApiOperation(value = "导入文件")
    public ResponseEntity doImportData(@RequestParam("file") MultipartFile file) {
        List<List<String>> list = new ArrayList<List<String>>();
        String filaName = file.getOriginalFilename();// 获取文件名
        String prefix = filaName.substring(filaName.lastIndexOf(".") + 1);// 获取文件后缀
        Map<String, Object> map = new HashMap<>(2);
        map.put("errno", 0);
        map.put("msg", "导入失败");
        try {
            InputStream in = file.getInputStream();
            if ("xlsx".equals(prefix.toLowerCase())) {
                list = IOUtil.importXlsxByType(in);// 导入xlsx格式
            } else if ("xls".equals(prefix.toLowerCase())) {
                list = IOUtil.importXlsByType(in);// 导入xls格式
            }
            if (list != null && list.size() > 0) {
                for (List<String> li : list) {
                    if (li.get(0) != null) {
                        YxProductBase yxProductBase = new YxProductBase();
                        yxProductBase.setName(li.get(0));
                        yxProductBase.setStatus(0);
                        yxProductBase.setAddTime(new Date());
                        yxProductBase.setCategory1(li.get(1));
                        yxProductBase.setCategory2(li.get(2));
                        yxProductBase.setCategory3(li.get(3));
                        yxProductBase.setSalesReference(Integer.parseInt(li.get(4)));
                        yxProductBase.setAdder(SecurityUtils.getUsername());
                        yxProductBaseService.create(yxProductBase);
                    }
                    map.put("errno", 1);
                    map.put("msg", "导入成功");
                }
            } else {
                map.put("msg", "导入文件是空文件，请重新导入");
            }
        } catch (Exception e) {
            e.printStackTrace();
            map.put("msg", "读取文件异常,请检查文件数据内容或请联系管理员");
        }

        return new ResponseEntity(map, HttpStatus.CREATED);
    }

    /**
     * 下载模版
     *
     * @param response
     * @throws IOException
     */
    @Log("下载模版")
    @GetMapping("/downloadTemplate")
    public void downloadTemplate(HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (int i = 1; i < 4; i++) {
            Map<String, Object> map = new LinkedHashMap<>();
            map.put("商品名称", "iphone" + i);
            map.put("一级分类", "一级分类" + i);
            map.put("二级分类", "二级分类" + i);
            map.put("三级分类", "三级分类" + i);
            map.put("销量参考",  + i);
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}
